//
//  Double+Extension.swift
//  LiveTrivia
//
//  Created by iOS on 2018/7/7.
//  Copyright © 2018年 LiveTrivia. All rights reserved.
//

import UIKit

extension Double {
    
    /// 货币格式化字符串
    ///
    /// - Parameters:
    ///   - symbol: 货币符号
    ///   - point: 最小小数点位数
    /// - Returns: 货币格式字符串
    func currency(_ symbol: String = "¥", point: Int = 0) -> String {
        let format = NumberFormatter()
        format.numberStyle = .currency                          //货币样式
        format.locale = Locale(identifier: "zh-Hans_CN")        //本地化
        format.currencySymbol = symbol                          //货币符号
        format.minimumFractionDigits = point                    //最少小数位
        format.maximumFractionDigits = point > 2 ? point : 2    //最大小数位
        let string = format.string(from: NSNumber(value: self))
        return string ?? "0"
    }
    
    /// 分 转 XX.XX万字符串
    var wanString: String {
        
        let format = NumberFormatter()
        format.numberStyle = .decimal
        format.locale = Locale(identifier: "zh-Hans_CN")
        
        let count = self / 100.0
        if count > 9999 {
            let w = count / 10000
            let tempS = w * 100
            // 把小数点后面的都去掉
            let tempI = Int(tempS)
            let temp = Double(tempI) / 100.0
            let tempW = format.string(from: NSNumber(value: temp)) ?? "0"
            return tempW + "万"
        } else {
            let string = format.string(from: NSNumber(value: count))
            return "\(string ?? "0")"
        }
    }
}
